package com.xuecheng.test.freemarker.controller;

import com.xuecheng.test.freemarker.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;

import java.util.*;

/**
 * Created by Aniymx on 2018/12/21 17:38.
 */
@Controller
@RequestMapping("/test")
public class FreemarkerController {

    @Autowired
    private RestTemplate restTemplate;




    @RequestMapping("/test01")
    public String test01(Map<String, Object> map) {
        Student student01 = new Student();
        student01.setName("zhangshan");
        student01.setAge(10);
        Student student02 = new Student();
        student01.setName("lisi");
        student01.setAge(10);
        Student student03 = new Student();
        student01.setName("wangwu");
        student01.setAge(10);

        map.put("name", "---->world!");
        map.put("student111", student01);

        ArrayList<Student> students = new ArrayList<>();
        students.add(student01);
        students.add(student02);
        students.add(student03);
        map.put("students",students);

        return "test1";
    }
    @GetMapping("/test02")
    public String test02(Map<String,Object> map){
        Student stu1 = new Student();
        stu1.setName("小明");
        stu1.setAge(20);
        stu1.setMoney(100000.0f);
        stu1.setBirthday(new Date());
        //map.put("stu1",stu1);
        Student stu2 = new Student();
        stu2.setName("小红");
        stu2.setAge(10);
        stu2.setMoney(100.0f);
        stu2.setBirthday(new Date());
        List<Student> friends = new ArrayList<>();
        friends.add(stu1);
        stu2.setList(friends);
        stu2.setBestFriend(stu1);
        List<Student> stus = new ArrayList<>();
        stus.add(stu1);
        stus.add(stu2);
        map.put("stus",stus);
        //准备map数据
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("stu1",stu1);
        hashMap.put("stu2",stu2);
        map.put("hashmap",hashMap);

        return "test1";
    }
@GetMapping("/banner")
    public String index_banner(Map<String,Object> map){

        ResponseEntity<Map> responseEntity = restTemplate.getForEntity("http://localhost:31001/cms/config/getmodel/5a791725dd573c3574ee333f", Map.class);
        Map body = responseEntity.getBody();
        map.putAll(body);

        return "index_banner";
    }

}
